package com.daikit.daikit4gxt.client.utils.miscs;

import com.daikit.commons.shared.miscs.Couple;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.TreeStore;


/**
 * Utility class for managing Store
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkStoreUtils
{

	/**
	 * Move item up
	 *
	 * @param store
	 *           the {@link ListStore}
	 * @param item
	 *           the item to move
	 */
	public static final <T> void moveUp(final ListStore<T> store, final T item)
	{
		final int position = store.indexOf(item);
		if (position > 0)
		{
			store.remove(item);
			store.add(position - 1, item);
		}
	}

	/**
	 * Move item up
	 *
	 * @param store
	 *           the {@link ListStore}
	 * @param item
	 *           the item to move
	 */
	public static final <T> void moveDown(final ListStore<T> store, final T item)
	{
		final int position = store.indexOf(item);
		if (position < store.size() - 1)
		{
			store.remove(item);
			store.add(position + 1, item);
		}
	}

	/**
	 * Add model to a {@link ListStore}
	 *
	 * @param store
	 *           the {@link ListStore}
	 * @param isCreation
	 *           boolean indicating whether this is a creation (true) or an update (false)
	 * @param model
	 *           the model to be added or updated
	 */
	public static final <T> void addModelToListStore(final ListStore<T> store, final boolean isCreation, final T model)
	{
		if (isCreation)
		{
			store.add(0, model);
		}
		else
		{
			store.update(model);
		}
	}

	/**
	 * Add model to a {@link TreeStore}
	 * 
	 * @param store
	 *           the {@link ListStore}
	 * @param isCreation
	 *           boolean indicating whether this is a creation (true) or an update (false)
	 * @param model
	 *           the model to be added or updated
	 * @param treeParentModel
	 *           the parent model in which to add the model
	 */
	public static final <T> void addModelToTreeStore(final TreeStore<T> store, final boolean isCreation, final T model, final T treeParentModel)
	{
		addModelToTreeStore(store, isCreation, model, treeParentModel, new TreeStoreModifierCallback<T>());
	}

	/**
	 * Add model to a {@link TreeStore}
	 * 
	 * @param store
	 *           the {@link ListStore}
	 * @param isCreation
	 *           boolean indicating whether this is a creation (true) or an update (false)
	 * @param model
	 *           the model to be added or updated
	 * @param treeParentModel
	 *           the parent model in which to add the model
	 * @param modifierCallback
	 *           the {@link TreeStoreModifierCallback}
	 */
	public static final <T> void addModelToTreeStore(final TreeStore<T> store, final boolean isCreation, final T model, final T treeParentModel,
			final TreeStoreModifierCallback<T> modifierCallback)
	{
		if (isCreation)
		{
			if (treeParentModel == null)
			{
				store.add(model);
			}
			else
			{
				modifierCallback.addModelToTreeStore(store, treeParentModel, -1, model);
			}
		}
		else
		{
			final int modelIndexInFormerParentModel = store.indexOf(model);
			final T formerParentModel = store.getParent(model);
			store.remove(model);
			final Couple<T, Integer> couple = modifierCallback.getNewParentModelAndIndexAfterSave(store, formerParentModel, modelIndexInFormerParentModel, model);
			modifierCallback.addModelToTreeStore(store, couple.first, couple.second, model);
		}
	}


}
